/**
 * Copyright (c) Huawei Technologies Co., Ltd. 1987-2021. All rights reserved.
*/

package com.gitee.fufu669.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import javax.crypto.*;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

// 注解
@SuppressWarnings({"restriction"})
/** @author wangfupeng */
/**
 * TODO
*/
public class CacheDesUtil {

    /**
     * TODO
    */
    public static final Logger logger = LoggerFactory.getLogger(CacheDesUtil.class);

    // TODO
    private byte[] desKey;

    /**
     * TODO
    */
    public CacheDesUtil(String desKey) {
        // 自动生成默认写入器写入属性
        this.desKey = desKey.getBytes();
    }

    /**
     * TODO
    */
    public static String base64Encode(byte[] s) {
        // 判断是否是空
        if (s == null) {
            // 返回
            return null;
        }
        // 创建
        BASE64Encoder b = new BASE64Encoder();
        // 返回
        return b.encode(s);
    }

    /**
     * TODO
    */
    public static byte[] base64Decode(String s) throws IOException {
        // 判断是否是空
        if (s == null) {
            // 返回
            return null;
        }
        // 创建
        BASE64Decoder decoder = new BASE64Decoder();
        // TODO
        byte[] b = decoder.decodeBuffer(s);
        // 返回
        return b;
    }

    /**
     * TODO
    */
    public byte[] desEncrypt(byte[] plainText) throws Exception {
        // 创建
        SecureRandom sr = new SecureRandom();
        // 赋值
        byte rawKeyData[] = desKey;
        // 创建
        DESKeySpec dks = new DESKeySpec(rawKeyData);
        // TODO
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        // TODO
        SecretKey key = keyFactory.generateSecret(dks);
        // TODO
        Cipher cipher = Cipher.getInstance("DES");
        // TODO
        cipher.init(Cipher.ENCRYPT_MODE, key, sr);
        // 赋值
        byte data[] = plainText;
        // TODO
        byte encryptedData[] = cipher.doFinal(data);
        // 返回
        return encryptedData;
    }

    /**
     * TODO
    */
    public byte[] desDecrypt(byte[] encryptText) throws Exception {
        // 创建
        SecureRandom sr = new SecureRandom();
        // 赋值
        byte rawKeyData[] = desKey;
        // 创建
        DESKeySpec dks = new DESKeySpec(rawKeyData);
        // TODO
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        // TODO
        SecretKey key = keyFactory.generateSecret(dks);
        // TODO
        Cipher cipher = Cipher.getInstance("DES");
        // TODO
        cipher.init(Cipher.DECRYPT_MODE, key, sr);
        // 赋值
        byte encryptedData[] = encryptText;
        // TODO
        byte decryptedData[] = cipher.doFinal(encryptedData);
        // 返回
        return decryptedData;
    }

    /**
     * TODO
    */
    public String encrypt(String input) throws Exception {
        // 返回
        return base64Encode(desEncrypt(input.getBytes()));
    }

    /**
     * TODO
    */
    public String decrypt(String input) throws Exception {
        // TODO
        byte[] result = base64Decode(input);
        // 返回
        return new String(desDecrypt(result));
    }

}  

